<template>
  <div class="password">
    <el-form
      :model="ruleForm"
      status-icon
      :rules="rules"
      ref="ruleForm"
      label-position="top"
      label-width="100px"
      class="ruleForm"
    >
      <el-form-item label="旧密码" prop="oldPassword">
        <el-input
          type="password"
          v-model="ruleForm.oldPassword"
          placeholder="请输入旧密码"
          autocomplete="off"
        ></el-input>
      </el-form-item>
      <el-form-item label="新密码" prop="newPassword">
        <el-input
          :type="passwordVisible ? 'text' : 'password'"
          v-model="ruleForm.newPassword"
          autocomplete="off"
          placeholder="请输入新密码"
        >
          <template slot="suffix">
            <span
              :class="passwordVisible ? 'el-icon-view' : 'el-icon-unview'"
              @click="togglePasswordVisibility()"
            ></span>
          </template>
        </el-input>
      </el-form-item>
      <el-form-item label="确认密码" prop="newPassword1">
        <el-input
          :type="checkPassVisible ? 'text' : 'password'"
          v-model="ruleForm.newPassword1"
          autocomplete="off"
          placeholder="请输入新密码"
        >
          <template slot="suffix">
            <span
              :class="checkPassVisible ? 'el-icon-view' : 'el-icon-unview'"
              @click="toggleCheckPasswordVisibility()"
            ></span>
          </template>
        </el-input>
      </el-form-item>

      <el-form-item>
        <el-button
          class="save-btn"
          type="primary"
          @click="submitForm('ruleForm')"
          >保存</el-button
        >
      </el-form-item>
    </el-form>
    <transition name="notification" v-if="isShow">
      <div class="popup-layout">
        <div class="layout-content">
          <img src="~@/assets/common/update_success.png" alt="" />
          <div class="layout-text">
            <div class="layout-title">密码修改成功</div>
            <!-- <div>{{ timeLeft }}s后自动返回登录界面</div> -->
          </div>
          <el-button class="save-btn" type="primary" @click="backLog()"
            >返回登录</el-button
          >
        </div>
      </div>
    </transition>
  </div>
</template>

<script>
import { updatePassword } from "@/api/system/possword";
import { getStore } from "@/utils/store";
// import md5 from "js-md5";
export default {
  name: "passwordBox",
  components: {},

  data() {
    var validatePass = (rule, value, callback) => {
      const regex =
        /^(?=.*[a-zA-Z])(?=.*\d)(?=.*[!@#$%^&*(),.?":{}|<>]).{8,16}$/;
      if (value === "") {
        callback(new Error("请输入新密码"));
      } else if (!regex.test(value)) {
        callback(new Error("密码必须包含字母加数字加特殊字符，长度为8-16位"));
      } else {
        if (this.ruleForm.newPassword1 !== "") {
          this.$refs.ruleForm.validateField("newPassword1");
        }
        callback();
      }
    };
    var validatePass1 = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入旧密码"));
      } else {
        callback();
      }
    };
    var validatePass2 = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请再次输入新密码"));
      } else if (value !== this.ruleForm.newPassword) {
        callback(new Error("两次输入密码不一致!"));
      } else {
        callback();
      }
    };
    return {
      ruleForm: {
        newPassword: "",
        newPassword1: "",
        oldPassword: "",
      },
      passwordVisible: false,
      checkPassVisible: false,
      rules: {
        newPassword: [{ validator: validatePass, trigger: "blur" }],
        newPassword1: [{ validator: validatePass2, trigger: "blur" }],
        oldPassword: [{ validator: validatePass1, trigger: "blur" }],
      },
      isShow: false,
      timeLeft: 5,
      countdownInterval: null,
    };
  },
  computed: {},
  mounted() {},
  watch: {
    timeLeft(newValue) {
      console.log(newValue);
      this.$forceUpdate();
    },
  },
  methods: {
    startCountdown() {
      // 设置为正在倒计时
      this.countdownInterval = setInterval(() => {
        if (this.timeLeft > 0) {
          this.timeLeft--; // 减少剩余时间
        } else {
          clearInterval(this.countdownInterval); // 清除定时器
        }
      }, 1000); // 每秒更新一次
    },
    togglePasswordVisibility() {
      this.passwordVisible = !this.passwordVisible;
    },
    toggleCheckPasswordVisibility() {
      this.checkPassVisible = !this.checkPassVisible;
    },
    async getUpdatePassword() {
      const userInfo = getStore({ name: "userInfo" });
      let data = await updatePassword(this.ruleForm, userInfo.user_id);

      if (data.data.code == 200) {
        this.isShow = true;
        this.$store.dispatch("FedLogOut").then(() => {});
      }
    },
    backLog() {
      this.$message(this.$t("login.logout"));
      this.$router.push("/login");
    },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.getUpdatePassword();
          // alert("submit!");
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
  },
};
</script>

<style lang="scss" scoped>
.password {
  width: 100%;
  height: 100%;
  border-radius: 8px;
  background-color: #fff;
  display: flex;
  align-items: center;
  // padding-top: 8%;
  justify-content: center;
  .ruleForm {
    width: 538px;
    height: 562px;
    background: url(~@/assets/common/password_bg.png) no-repeat;
    background-size: cover;
    padding: 0 64px;
    display: flex;
    flex-direction: column;
    justify-content: center;
  }
  .el-button {
    width: 100%;
    height: 48px;
    font-size: 16px;
    background-color: #0087d6;
    border-color: #0087d6;
  }
  ::v-deep .el-input__suffix-inner {
    display: flex;
    align-items: center;
    .el-icon-unview {
      width: 24px;
      height: 24px;
      background: url("~@/assets/common/unview.png");
      margin-right: 10px;
    }
  }
}
::v-deep .el-form-item {
  margin-bottom: 32px;
  .el-button {
    margin-top: 16px;
  }
}

.popup-layout {
  position: fixed;
  width: 100vw;
  height: 100vh;
  background: rgba(#000, 0.4);
  z-index: 2;
  top: 0;
  left: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all 0.5s;

  .layout-content {
    padding: 24px;
    display: flex;
    width: 560px;
    height: 357px;
    flex-direction: column;
    background: #fff;
    align-items: center;
    justify-content: center;
    border-radius: 8px;
    box-shadow: 0px 2px 4px -1px rgba(0, 0, 0, 0.12),
      0px 4px 5px 0px rgba(0, 0, 0, 0.08), 0px 1px 10px 0px rgba(0, 0, 0, 0.05);
    img {
      width: 140px;
      height: auto;
      margin-bottom: 12px;
    }
    .layout-title {
      font-size: 18px;
      font-weight: 600;
      margin: 8px;
      color: #000;
      text-align: center;
      margin-bottom: 40px;
    }
    .layout-text {
      font-size: 14px;
      color: rgba(0, 0, 0, 0.4);
    }
  }
}
::v-deep {
  .el-form-item__label {
    font-size: 16px;
    font-weight: 600;
    color: #000000;
  }
  .el-form-item__error {
    color: #e34d59;
  }
  .el-form-item.is-error .el-input__inner {
    border-color: #e34d59;
  }
  .el-form-item--feedback .el-input__validateIcon {
    display: none;
  }
}
::v-deep .el-input {
  .el-input__inner {
    height: 40px;
  }
  .el-icon-view {
    font-size: 18px;
    width: 20px;
    height: 20px;
    text-align: left;
    display: inline-block;
    color: #bcc4d0;
    margin-right: 11px;
  }
  .el-icon-circle-close {
    display: none;
  }
}
::v-deep.save-btn:hover {
  background-color: #4e52da;
  border-color: #4e52da;
}
</style>
